import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/components/login'
import index from '@/components/index'
import home from '@/components/home'
import users from '@/components/users'
import roles from '@/components/roles'
import rights from '@/components/rights'
import goods from '@/components/goods'
import goodsAdd from '@/components/goodsAdd'
import params from '@/components/params'
import categories from '@/components/categories'
import orders from '@/components/orders'
import reports from '@/components/reports'
import sellerpage from '@/views/sellerpage'
import trendpage from '@/views/trendpage'
import mappage from '@/views/mappage'
import rankpage from '@/views/rankpage'
import hotpage from '@/views/hotpage'
import stockpage from '@/views/stockpage'
import screenpage from '@/views/screenpage'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  component: login
}, {
  path: '/index',
  component: index,
  children: [{
    path: '/',
    redirect: '/home'
  }, {
    path: '/home',
    component: home
  }, {
    path: '/users',
    component: users
  }, {
    path: '/roles',
    component: roles
  }, {
    path: '/rights',
    component: rights
  }, {
    path: '/goods',
    component: goods
  }, {
    path: '/goodsAdd',
    component: goodsAdd
  }, {
    path: '/params',
    component: params
  }, {
    path: '/categories',
    component: categories
  }, {
    path: '/orders',
    component: orders
  }, {
    path: '/reports',
    component: reports
  }]
}, {
  path: '/index/screenpage',
  component: screenpage
}, {
  path: '/index/sellerpage',
  component: sellerpage
}, {
  path: '/index/trendpage',
  component: trendpage
}, {
  path: '/index/mappage',
  component: mappage
}, {
  path: '/index/rankpage',
  component: rankpage
}, {
  path: '/index/hotpage',
  component: hotpage
}, {
  path: '/index/stockpage',
  component: stockpage
}]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
